package com.boscloud.controller;

import com.boscloud.pojo.Result;
import com.boscloud.pojo.User;
import com.boscloud.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@CrossOrigin //允许跨域
public class LoginController {
    @Autowired
    private UserService userservice;
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        //登陆验证
       User us = userservice.login(user);

        String jwt = "";
       //获取JWT令牌
        if(us != null) {
            jwt = userservice.getJwt(us);

        }
       return us !=null?Result.success(jwt,us.getPlant(),us.getFirstName(),us.getErpId()):Result.error("401","用户名或密码错误");
    }
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody User user){
      int result = userservice.changePwd(user);
      if( result == 1){
          return Result.success("",user.getPlant(),user.getFirstName(),user.getErpId());
      }else{
          return Result.error("500","修改失败用户密码错误");

      }
    }
}
